package com.sky.web.admin;

import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersConfirmDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.entity.Orders;
import com.sky.result.PageBean;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    //订单搜索
    @GetMapping("/conditionSearch")
    public Result conditionSearch(OrdersPageQueryDTO ordersPageQueryDTO){
        PageBean pageBean = orderService.findByPage(ordersPageQueryDTO);
        return Result.success(pageBean);
    }

    //各个状态的订单数量统计
    @GetMapping("/statistics")
    public Result statistics(){
        OrderStatisticsVO orderStatisticsVO = orderService.statistics();
        return Result.success(orderStatisticsVO);
    }

    // 查询订单详情  (controller mapper 直接复用app的查看订单详情即可)
    @GetMapping("/details/{id}")
    public Result details(@PathVariable("id") Long id){
        OrderVO orderVO = orderService.findById(id);
        return Result.success(orderVO);
    }

    // 接单 (把订单状态2 改为3 也只能 把订单状态2 改为3 所以要做判断)
    @PutMapping("/confirm")
    public Result confirm(@RequestBody OrdersConfirmDTO ordersConfirmDTO){
        orderService.confirm(ordersConfirmDTO);
        return Result.success();
    }

    //拒单 (把订单状态2 改为6 也只能 把订单状态2 改为6 所以要做判断  这是商户拒单)
    @PutMapping("/rejection")
    public Result rejection(@RequestBody OrdersRejectionDTO ordersRejectionDTO){
        orderService.rejection(ordersRejectionDTO);
        return Result.success();
    }

    // 取消订单 (- 商家取消订单时需要指定取消原因，使用OrdersCancelDTO接收
                  //- 只有订单处于`1待付款 2待接单 3已接单 4派送中 `状态时可以执行取消订单操作)
    @PutMapping("/cancel")
    public Result cancel(@RequestBody OrdersCancelDTO ordersCancelDTO){
        orderService.cancel(ordersCancelDTO);
        return Result.success();
    }

    // 派送订单 (派送订单其实就是将订单状态修改为`派送中`  3 改为 4)
    @PutMapping("/delivery/{id}")
    public Result delivery(@PathVariable("id") Long id) {
        orderService.delivery(id);
        return Result.success();
    }

    // 完成订单 (完成订单其实就是将订单状态: 派送中修改为`已完成`  4 改为 5)
    @PutMapping("/complete/{id}")
    public Result complete(@PathVariable("id") Long id) {
        orderService.complete(id);
        return Result.success();
    }

}
